package com.chinasoft.reposearch.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinasoft.reposearch.entity.ResponseResult;
import com.chinasoft.reposearch.entity.dto.LanguageCountDto;
import com.chinasoft.reposearch.entity.po.LanguageCount;
import com.chinasoft.reposearch.mapper.LanguageCountMapper;
import com.chinasoft.reposearch.service.LanguageCountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class LanguageCountServiceImpl implements LanguageCountService {
    private final LanguageCountMapper languageCountMapper;
//    @Autowired
//    public LanguageCountServiceImpl(LanguageCountMapper languageCountMapper) {
//        this.languageCountMapper = languageCountMapper;
//    }


    /**
     * 查询所有语言 某个年月的 列表和数目
     */
    @Override
    public ResponseResult getLanguageCount(Integer year, Integer month) {
//        if(year == null&& month!=null){
//            return ResponseResult.success(null);
//        }
//
//        List<LanguageCount> languageCountList = null;
//        if(year != null&&month==null){
//            languageCountList = languageCountMapper.selectList(
//                    Wrappers.<LanguageCount>lambdaQuery()
//                            .eq(LanguageCount::getYear, year)
//            );
//        } else if(year != null&&month!=null){
//            languageCountList = languageCountMapper.selectList(
//                    Wrappers.<LanguageCount>lambdaQuery()
//                            .eq(LanguageCount::getYear, year)
//                            .eq(LanguageCount::getMonth, month)
//            );
//        }else{
//            languageCountList = languageCountMapper.selectList(
//                    Wrappers.<LanguageCount>lambdaQuery()
//            );
//        }

        List<LanguageCount> languageCountList = languageCountMapper.selectList(
                Wrappers.<LanguageCount>lambdaQuery()
                        // 仅当year不为null时，才添加year条件
                        .eq(year != null, LanguageCount::getYear, year)
                        // 仅当month不为null时，才添加month条件
                        .eq(month != null, LanguageCount::getMonth, month)
        );
        LanguageCountDto languageCountDto = new LanguageCountDto(languageCountList.size(), languageCountList);
        return ResponseResult.success(languageCountDto);
    }


    /**
     * 查询某个语言 所有年月的 列表和数目
     */
    @Override
    public List<LanguageCount> getLanguageTrending(String language, Integer year) {
        List<LanguageCount> languageTrendingList = languageCountMapper.selectList(
                Wrappers.<LanguageCount>lambdaQuery()
                        .eq(LanguageCount::getLanguage, language)
                        .eq(LanguageCount::getYear, year)
        );
        LanguageCountDto languageCountDto = new LanguageCountDto(languageTrendingList.size(), languageTrendingList);
        return languageTrendingList;
    }

    @Override
    public List<String> getLanguages() {
        List<LanguageCount> list = languageCountMapper.selectList(
                Wrappers.<LanguageCount>lambdaQuery()
                        .select(LanguageCount::getId, LanguageCount::getLanguage)
                        .groupBy(LanguageCount::getLanguage)
        );
        return list.stream().map(LanguageCount::getLanguage).toList();
    }

    @Override
    public List<LanguageCount> getLanguageTrendingYears(String language, Integer start, Integer end) {
        return languageCountMapper.selectListYears(language, start, end);
    }
}
